package api

import (
	"context"
	"fmt"
	"strconv"

	"gitee.com/go-micro-services/api/utils"
	cart "gitee.com/go-micro-services/cart/proto/cart"
	"gitee.com/go-micro-services/common"
	"github.com/gin-gonic/gin"

	"github.com/prometheus/common/log"
)

type Log2Controller struct{}

// 通用配置
var ZapLogger *common.ZapLogger

func init() {
	ZapLogger = common.NewZapLogger("logs/app2.log", 0777) // 这里相关参数可以配置到 conf/app.ini 中
}

// 这个方法用于测试ELK
func (con Log2Controller) FindAllTestElk(c *gin.Context) {
	log.Info("接受到 /api/findAllTestElk2 访问请求")
	// 1. 获取参数
	user_id_str := c.Query("user_id")
	userId, err := strconv.ParseInt(user_id_str, 10, 64)
	if err != nil {
		ZapLogger.Error("参数异常")
		c.JSON(200, gin.H{
			"message": "参数异常",
			"success": false,
		})
		return
	}
	fmt.Println(userId)
	// 2. rpc 远程调用：获取购物车所有商品
	cartClient := cart.NewCartService(utils.CartServices, utils.SrvClient)
	cartAll, err := cartClient.GetAll(context.TODO(), &cart.CartFindAll{UserId: userId})

	ZapLogger.Info(cartAll)
	fmt.Println("-----")

	c.JSON(200, gin.H{
		"data":    cartAll,
		"success": true,
	})
}
